﻿using System.Collections;
using System.Collections.Generic;
using System;
using System.Text;

using System.IO;
using System.Data;
using System.Xml;
using System.Web.Security;
using System.Security.Cryptography;
using System.Net;
using System.Web;
using System.Management;


namespace Gift.WebApp
{

    public class SoftReg
    {
        // Fields
        public char[] Charcode = new char[0x19];
        public int[] intCode = new int[0x7f];
        public int[] intNumber = new int[0x19];

        // Methods
        public string EncryptionMD5(string encryptionString)
        {
            MD5 md = MD5.Create();
            byte[] buffer = new byte[0x10];
            ASCIIEncoding encoding = new ASCIIEncoding();
            return Convert.ToBase64String(md.ComputeHash(encoding.GetBytes(encryptionString)));
        }

        public string getCpu()
        {
            ManagementObjectCollection instances = new ManagementClass("win32_Processor").GetInstances();
            foreach (ManagementObject obj2 in instances)
            {
                return obj2.Properties["Processorid"].Value.ToString().Substring(4, 8);
            }
            return null;
        }

        public string GetDiskVolumeSerialNumber()
        {
            ManagementClass class2 = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObject obj2 = new ManagementObject("win32_logicaldisk.deviceid=\"d:\"");
            obj2.Get();
            return obj2.GetPropertyValue("VolumeSerialNumber").ToString().Substring(4, 4);
        }

        public string getMNum()
        {
            return (this.getCpu() + this.GetDiskVolumeSerialNumber() + this.GetPath()).Substring(0, 0x18);
        }

        public string GetPath()
        {
            string encryptionString = "";
            try
            {
                encryptionString = HttpContext.Current.Server.MapPath("/");
                string str2 = this.EncryptionMD5(encryptionString).ToUpper();
                encryptionString = this.EncryptionMD5(encryptionString).ToUpper().Substring(0, 12);
            }
            catch
            {
            }
            return encryptionString;
        }

        public string getRNum()
        {
            int num2;
            this.setIntCode();
            string str = this.getMNum();
            for (int i = 1; i < this.Charcode.Length; i++)
            {
                this.Charcode[i] = Convert.ToChar(str.Substring(i - 1, 1));
            }
            for (num2 = 1; num2 < this.intNumber.Length; num2++)
            {
                this.intNumber[num2] = this.intCode[Convert.ToInt32(this.Charcode[num2])] + Convert.ToInt32(this.Charcode[num2]);
            }
            string str2 = "";
            for (num2 = 1; num2 < this.intNumber.Length; num2++)
            {
                if ((((str2.Length == 4) || (str2.Length == 9)) || ((str2.Length == 14) || (str2.Length == 0x13))) || (str2.Length == 0x18))
                {
                    str2 = str2 + "-";
                }
                if ((this.intNumber[num2] >= 0x30) && (this.intNumber[num2] <= 0x39))
                {
                    str2 = str2 + Convert.ToChar(this.intNumber[num2]).ToString();
                }
                else if ((this.intNumber[num2] >= 0x41) && (this.intNumber[num2] <= 90))
                {
                    str2 = str2 + Convert.ToChar(this.intNumber[num2]).ToString();
                }
                else if ((this.intNumber[num2] >= 0x61) && (this.intNumber[num2] <= 0x7a))
                {
                    str2 = str2 + Convert.ToChar(this.intNumber[num2]).ToString();
                }
                else if (this.intNumber[num2] > 0x7a)
                {
                    str2 = str2 + Convert.ToChar((int)(this.intNumber[num2] - 10)).ToString();
                }
                else
                {
                    str2 = str2 + Convert.ToChar((int)(this.intNumber[num2] - 9)).ToString();
                }
            }
            return str2;
        }

        public string getRNumByMNum(string mNum)
        {
            int num2;
            this.setIntCode();
            string str = mNum;
            for (int i = 1; i < this.Charcode.Length; i++)
            {
                this.Charcode[i] = Convert.ToChar(str.Substring(i - 1, 1));
            }
            for (num2 = 1; num2 < this.intNumber.Length; num2++)
            {
                this.intNumber[num2] = this.intCode[Convert.ToInt32(this.Charcode[num2])] + Convert.ToInt32(this.Charcode[num2]);
            }
            string str2 = "";
            for (num2 = 1; num2 < this.intNumber.Length; num2++)
            {
                if ((((str2.Length == 4) || (str2.Length == 9)) || ((str2.Length == 14) || (str2.Length == 0x13))) || (str2.Length == 0x18))
                {
                    str2 = str2 + "-";
                }
                if ((this.intNumber[num2] >= 0x30) && (this.intNumber[num2] <= 0x39))
                {
                    str2 = str2 + Convert.ToChar(this.intNumber[num2]).ToString();
                }
                else if ((this.intNumber[num2] >= 0x41) && (this.intNumber[num2] <= 90))
                {
                    str2 = str2 + Convert.ToChar(this.intNumber[num2]).ToString();
                }
                else if ((this.intNumber[num2] >= 0x61) && (this.intNumber[num2] <= 0x7a))
                {
                    str2 = str2 + Convert.ToChar(this.intNumber[num2]).ToString();
                }
                else if (this.intNumber[num2] > 0x7a)
                {
                    str2 = str2 + Convert.ToChar((int)(this.intNumber[num2] - 10)).ToString();
                }
                else
                {
                    str2 = str2 + Convert.ToChar((int)(this.intNumber[num2] - 9)).ToString();
                }
            }
            return str2;
        }

        public void setIntCode()
        {
            for (int i = 1; i < this.intCode.Length; i++)
            {
                this.intCode[i] = i % 9;
            }
        }
    }
}



